<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="关键字" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="用户名"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery()">
            <template #icon>
              <Search />
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery()">
            <template #icon>
              <Refresh />
              重置
            </template>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-wrapper">
      <el-table v-loading="loading" :data="tableData" row-key="uuid" border>
        <el-table-column
          prop="userId"
          label="用户id"
          width="70"
          align="center"
        />
        <el-table-column prop="username" label="用户名" min-width="100" />
        <el-table-column prop="ip" label="登录IP" align="center" />
        <el-table-column prop="location" label="登录地址" />
        <el-table-column prop="browser" label="浏览器" />
        <el-table-column prop="os" label="操作系统" />
        <el-table-column prop="status" label="在线状态" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? "在线" : "离线" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="登录时间" width="160" />
        <el-table-column prop="expireTime" label="过期时间" width="160" />
        <el-table-column label="操作" fixed="right" align="left" width="100">
          <template #default="scope">
            <el-button
              v-if="scope.row.status === 1"
              v-hasPerm="['sys:online:user:delete']"
              type="danger"
              link
              size="small"
              @click.stop="handleDelete(scope.row.uuid, scope.row.username)"
            >
              <template #icon>
                <Delete />
              </template>
              强退
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "OnlineUser",
  inheritAttrs: false,
});

import OnlineUserAPI, {
  OnlineUserPageVO,
  OnlineUserPageQuery,
} from "@/api/system/online-user";

const queryFormRef = ref(ElForm);
const loading = ref(false);
const tableData = ref<OnlineUserPageVO[]>();

const total = ref(0);
const queryParams = reactive<OnlineUserPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 查询在线用户 */
function handleQuery() {
  loading.value = true;
  OnlineUserAPI.getPage(queryParams)
    .then((data) => {
      tableData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 删除在线用户 */
function handleDelete(uuid: string, username: string) {
  ElMessageBox.confirm("确认强退用户【" + username + "】数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      OnlineUserAPI.deleteById(uuid)
        .then(() => {
          ElMessage.success("强退成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    () => {
      ElMessage.info("已取消强退");
    }
  );
}

onMounted(() => {
  handleQuery();
});
</script>
